var app = getApp();
Page({
  data: {
    select: false,
    pery: true,
    hid: false,
    hid1: true,
    checkInDate: '',
    checkOutDate: '',
    checkInDateText: '',
    checkOutDateText: '',
    room: {},
    price: {},
    hotel_id:0,
    room_id:0,
    realname:'',
    phone:'',
    email:'',
    room_nums:'1',
    hotel:{}
  },
  loadDetail(id) {
    var self = this
    app.request({
      url: '/home/hotel/detail',
      method: 'POST',
      data: {
        id: id
      },
      success: (res) => {
        console.log(res)
        self.setData({
          hotel: res.result
        })
      }
    })
  },
  realNameCheck(){
    if (this.data.realname == '') {
      wx.showToast({
        title: '请输入您的名字',
        icon:'none'
      })
      return false
    } else if (this.data.realname.length > 48) {
      wx.showToast({
        title: '您的名字不能超过48个字符',
        icon: 'none'
      })
      return false
    } else if (!/^[\u0391-\uFFE5A-Za-z]+$/.test(this.data.realname)) {
      wx.showToast({
        title: '只能输入中文或英文',
        icon: 'none'
      })
      return false
    } else {
      return true
    }
  },
  realNameInput(e) {
    this.setData({
      realname: e.detail.value
    })
  },
  phoneCheck(){
    if (this.data.phone == '') {
      wx.showToast({
        title: '请输入电话',
        icon: 'none'
      })
      return false
    } else if (!/\d/.test(this.data.phone)) {
      wx.showToast({
        title: '请正确输入手机号码',
        icon: 'none'
      })
      return false
    } else if (this.data.phone.length > 11 || this.data.phone.length < 11) {
      wx.showToast({
        title: '请正确输入手机号码',
        icon: 'none'
      })
      return false
    } else {
      return true
    }
  },
  phoneInput(e){
    this.setData({
      phone: e.detail.value
    })
  },
  emailCheck(){
    if (this.data.email != '') {
      var reMail = /^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/;
      if (!reMail.test(this.data.email)){
        wx.showToast({
          title: '请正确输入邮箱',
          icon: 'none'
        })
        return false;
      }
    }
    return true
  },
  emailInput(e) {
    this.setData({
      email: e.detail.value
    })
  },
  change1: function () {
    var pery = this.data.pery;
    this.setData({
      pery: true,
      hid: false,
      hid1: true
    })
  },
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  mySelect(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      room_nums: name,
      select: false
    })
    this.loadDetail()
  },
  onLoad: function (options) {
    var self = this
    if (options && options.hotelid && options.roomid) {
      self.setData({
        hotel_id: options.hotelid,
        room_id: options.roomid
      })
    }
    this.loadDetail(this.data.hotel_id)
    //今天的时间
    var today = new Date();
    var todayDate = today.getFullYear() + "-" + ((today.getMonth() + 1) < 10 ? "0" + (today.getMonth() + 1) : (today.getMonth() + 1)) + "-" + today.getDate();
    //明天的时间
    var tomorrow = new Date();
    tomorrow.setTime(tomorrow.getTime() + 24 * 60 * 60 * 1000);
    var tomorrowDate = tomorrow.getFullYear() + "-" + ((tomorrow.getMonth() + 1) < 10 ? "0" + (tomorrow.getMonth() + 1) : (tomorrow.getMonth() + 1)) + "-" + tomorrow.getDate();
    wx.getStorage({
      key: 'ROOM_SOURCE_DATE',
      success: function (res) {
        console.log(res.data.checkInDate)
        console.log(res.data.checkOutDate)
        if (Date.parse(res.data.checkInDate) - Date.parse(todayDate) < 0) {
          self.setData({
            checkInDate: todayDate,
            checkOutDate: tomorrowDate,
            checkInDateText: self.getDateText(todayDate),
            checkOutDateText: self.getDateText(tomorrowDate)
          })
        } else {
          self.setData({
            checkInDate: res.data.checkInDate,
            checkOutDate: res.data.checkOutDate,
            checkInDateText: self.getDateText(res.data.checkInDate),
            checkOutDateText: self.getDateText(res.data.checkOutDate)
          })
        }
        self.loadDetail()
      },
      fail: function (res) {
        console.log("缓存中没有日期")
        //设置今天和明天
        self.setData({
          checkInDate: todayDate,
          checkOutDate: tomorrowDate,
          checkInDateText: self.getDateText(todayDate),
          checkOutDateText: self.getDateText(tomorrowDate)
        })
        self.loadDetail()
      }
    })

  },
  loadDetail(){
    var self = this
    app.request({
      url: '/home/order/getDetail',
      method: 'post',
      data: {
        room_id: self.data.room_id,
        checkin_time: self.data.checkInDate,
        checkout_time: self.data.checkOutDate,
        room_num: self.data.room_nums
      },
      success: (res) => {
        console.log(res)
        self.setData({
          room: res.result.room,
          price: res.result.price
        })
        wx.setNavigationBarTitle({
          title: res.result.room.room_name
        })
      }
    })
  },
  callPhone(e) {
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
      success: () => {
      }
    })
  },
  change: function (e) {
    var pery = this.data.pery;
    if (pery == true) {
      this.setData({
        pery: false,
        hid: true,
        hid1: false
      })
    } else {
      this.setData({
        pery: true,
        hid: false,
        hid1: true
      })
    }
  },
  onShow: function () {
  },
  submit() {
    if (this.realNameCheck() && this.phoneCheck() && this.emailCheck()) {
      let self = this;
      wx.getStorage({
        key: 'token',
        success: function (res) {
          var member_id = res.data.member_id
          var openid = res.data.openid
          app.request({
            url: '/home/order/add',
            method: 'post',
            data: {
              member_id: member_id,
              hotel_id: self.data.hotel_id,
              room_id: self.data.room_id,
              realname: self.data.realname,
              phone: self.data.phone,
              email: self.data.email,
              room_nums: self.data.room_nums,
              room_price: self.data.room.room_price,
              checkin_time: self.data.checkInDate,
              checkout_time: self.data.checkOutDate
            },
            success: (res) => {
              console.log(res)
              if (res && res.result) {
                var order_id = res.result.order_id
                wx.redirectTo({
                  url: '../pay/index?order_id=' + order_id,
                })
              }
            }
          })
        },
        fail: (res) => {
          wx.navigateTo({
            url: '/page/component/login/index',
          })
        }
      })
    }
    
  },
  getDateText(string) {
    var date = new Date(string);
    return (date.getMonth() + 1) + "月" + date.getDate() + "日"
  }
})